package com.ai.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.ai.exception.BusinessException;
import com.ai.exception.code.BaseResponseCode;
import com.ai.filter.aop.annotation.LogAnnotation;
import com.ai.service.HomeService;
import com.ai.service.UserRoleService;
import com.ai.service.UserService;
import com.ai.utils.CaptchaUtils;
import com.ai.utils.SatokenUtils;
import com.ai.web.PageResp;
import com.ai.web.request.*;
import com.ai.web.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * 用户管理
 */
@RestController
@Api(tags = "组织模块-用户管理")
@RequestMapping("/sys")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private HomeService homeService;

    /**
     * 获取验证码图片
     * @param request
     * @param response
     */
    @RequestMapping("/getVerify")
    public void getCaptchaCode(HttpServletRequest request, HttpServletResponse response) {
        CaptchaUtils.outCaptcha(request,response);
    }

    /**
     * 用户登录接口
     * @param loginReq
     * @param request
     * @return
     */
    @PostMapping(value = "/user/login")
    @ApiOperation(value = "用户登录接口")
    public LoginResp login(@RequestBody @Valid LoginReq loginReq, HttpServletRequest request) {
        CaptchaUtils.verCaptcha(request,loginReq.getCaptcha());
        return userService.login(loginReq);
    }

    @GetMapping("/home")
    @ApiOperation(value = "获取首页数据接口")
    public HomeResp getHomeInfo() {
        String userId =SatokenUtils.getLoginIdAsString();
        return homeService.getHomeInfo(userId);
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "用户注册接口")
    public void register(@RequestBody @Valid UserAddReq userAddReq) {
        userService.register(userAddReq);
    }

    @GetMapping("/user/unLogin")
    @ApiOperation(value = "引导客户端去登录")
    public void unLogin() {
        throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
    }

    @PutMapping("/user")
    @ApiOperation(value = "更新用户信息接口")
    @LogAnnotation(title = "用户管理", action = "更新用户信息")
    @SaCheckPermission("sys:user:update")
    public void updateUserInfo(@RequestBody UserUpdateReq userUpdateReq) {
        userService.updateUserInfo(userUpdateReq);
    }

    @PutMapping("/user/info")
    @ApiOperation(value = "更新用户信息接口")
    @LogAnnotation(title = "用户管理", action = "更新用户信息")
    public void updateUserInfoById(@RequestBody UserUpdateReq userUpdateReq) {
        userService.updateUserInfoMy(userUpdateReq);
    }

    @GetMapping("/user/{id}")
    @ApiOperation(value = "查询用户详情接口")
    @LogAnnotation(title = "用户管理", action = "查询用户详情")
    @SaCheckPermission("sys:user:detail")
    public UserDetailResp detailInfo(@PathVariable("id") String id) {
        return userService.findById(id);
    }

    @GetMapping("/user")
    @ApiOperation(value = "查询用户详情接口")
    @LogAnnotation(title = "用户管理", action = "查询用户详情")
    public UserDetailResp youSelfInfo() {
        String userId = StpUtil.getLoginIdAsString();
        return userService.findById(userId);
    }

    @PostMapping("/users")
    @ApiOperation(value = "分页获取用户列表接口")
    @SaCheckPermission("sys:user:list")
    @LogAnnotation(title = "用户管理", action = "分页获取用户列表")
    public PageResp<UserPageResp> pageInfo(@RequestBody UserPageReq userPageReq) {
        return userService.pageInfo(userPageReq);
    }

    @PostMapping("/user")
    @ApiOperation(value = "新增用户接口")
    @SaCheckPermission("sys:user:add")
    @LogAnnotation(title = "用户管理", action = "新增用户")
    public void addUser(@RequestBody @Valid UserAddReq userAddReq) {
        userService.addUser(userAddReq);
    }

    @GetMapping("/user/logout")
    @ApiOperation(value = "退出接口")
    public void logout() {
        StpUtil.logout();
    }

    @PutMapping("/user/pwd")
    @ApiOperation(value = "修改密码接口")
    @LogAnnotation(title = "用户管理", action = "更新密码")
    public void updatePwd(@RequestBody UserUpdatePwdReq userUpdatePwdReq) {
        userUpdatePwdReq.setId(StpUtil.getLoginIdAsString());
        userService.updatePwd(userUpdatePwdReq);
    }

    @DeleteMapping("/user")
    @ApiOperation(value = "删除用户接口")
    @LogAnnotation(title = "用户管理", action = "删除用户")
    @SaCheckPermission("sys:user:deleted")
    public void deletedUser(@RequestBody @ApiParam(value = "用户id集合") List<String> userIds) {
        userService.deleteUser(userIds);
    }

    @GetMapping("/user/roles/{userId}")
    @ApiOperation(value = "赋予角色-获取所有角色接口")
    @LogAnnotation(title = "用户管理", action = "赋予角色-获取所有角色接口")
    @SaCheckPermission("sys:user:role:detail")
    public UserOwnRoleResp getUserOwnRole(@PathVariable("userId") String userId) {
        return userService.getUserOwnRole(userId);
    }

    @PutMapping("/user/roles/{userId}")
    @ApiOperation(value = "赋予角色-用户赋予角色接口")
    @LogAnnotation(title = "用户管理", action = "赋予角色-用户赋予角色接口")
    @SaCheckPermission("sys:user:role:update")
    public void addUserRole(@PathVariable("userId") String userId, @RequestBody List<String> roleIds) {
        userRoleService.addUserRole(userId,roleIds);
    }

}
